<template>
  <div style="height: 100%; width: 100%; position: relative; overflow: hidden">
      <div class="pagetop">
          <div class="top-deal">
              <el-button icon="el-icon-refresh" type="primary" @click="InitData()" v-has="'PINQUERY'" :disabled="changeflag">刷新</el-button>
              <el-button icon="el-icon-plus" @click="AddDetail()" v-has="'PADD'">新增</el-button>
              <el-button icon="el-icon-delete" @click="DelDetail()" v-has="'PDELETE'">删除</el-button>
              <el-button icon="el-icon-document" @click="SaveAction()" v-has="'PDELETE'" :disabled="!changeflag">保存</el-button>
              <el-button icon="el-icon-close" @click="Cancel()" v-has="'PDELETE'" :disabled="!changeflag">取消</el-button>
              <el-button icon="el-icon-document" @click="Export()" v-has="'PDELETE'" :disabled="changeflag">导出</el-button>
              <el-button icon="el-icon-document" @click="HelpInit()">帮助</el-button>
              <!-- <el-button @click="btnImport" icon="el-icon-download">导入</el-button> -->
          </div>
      </div>
      <div class="pagecenter">
          <vxe-table show-overflow ref="lxTable" :data.sync="autonoruleData" height="auto" :loading="autonoruleLoading" :keyboard-config="keyConfig" :keyboard-local-config="keyLocalConfig" :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true, showIcon: false }" @edit-closed="EditClose" border size="mini" show-header-overflow highlight-current-row highlight-hover-row resizable header-align="center" :edit-rules="validRules" :row-class-name="rowClassName" @cell-dblclick="curtable_celldblclick">
              <vxe-table-column field="seq" title="序号" width="60px" align="center" :edit-render="{ name: 'input', events: { change: hasChange } }"></vxe-table-column>
              <vxe-table-column field="rptunitname" title="报告单元" width="200" align="center" header-align="center"></vxe-table-column>
              <vxe-table-column field="pat_typecodename" title="病人类型" width="120" align="center" header-align="center" :formatter="format">
              </vxe-table-column>
              <vxe-table-column field="emer_flag" title="急诊类型" align="center" width="100px" :edit-render="{ name: 'select', options: emerflagoption }" sortable></vxe-table-column>
              <vxe-table-column field="specimen_name" title="标本类型" width="145" align="center" header-align="center" :formatter="format"></vxe-table-column>
              <vxe-table-column field="reqitemgroups" title="申请项目编号归类组合" align="center" width="200px" :edit-render="{ name: 'input', events: { change: hasChange } }" sortable></vxe-table-column>
              <vxe-table-column field="destreqitemgroups" title="要编号的归类组合" align="center" width="110px" :edit-render="{ name: 'input', events: { change: hasChange } }" sortable></vxe-table-column>
              <vxe-table-column field="sampleno1" title="开始样本号" align="center" width="100px" :edit-render="{ name: 'input', events: { change: hasChange } }" sortable></vxe-table-column>
              <vxe-table-column field="sampleno2" title="结束样本号" align="center" width="100px" :edit-render="{ name: 'input', events: { change: hasChange } }" sortable></vxe-table-column>
              <vxe-table-column field="samplenoformat" title="样本号规则" align="center" width="120px" :edit-render="{ name: 'input', events: { change: hasChange } }" sortable></vxe-table-column>
              <vxe-table-column field="matchby" title="匹配依据" align="center" width="120px" :edit-render="{ name: 'select', options: matchbyoption, events: { change: matchbyChange } }" sortable></vxe-table-column>
              <vxe-table-column key="weekdays" field="weekdays" title="适用周几检验" :edit-render="{ name: 'input', events: { change: hasChange }}" width="100px"></vxe-table-column>
              <vxe-table-column key="starttime" field="starttime" title="适用当日开始时间" :edit-render="{ name: 'input', events: { change: hasChange }}" width="210px">
                  <template v-slot:edit="{ row }">
                      <el-input style="width:100%;" @focus="setstarttime($event,row)" v-model="row.starttime" placeholder="请输入开始时间" clearable="" :readonly="true"></el-input>
                  </template>
              </vxe-table-column>
              <vxe-table-column key="endtime" field="endtime" title="适用当日截止时间" :edit-render="{ name: 'input', events: { change: hasChange }}" width="210px">
                  <template v-slot:edit="{ row }">
                      <el-input style="width:100%;" @focus="setendtime($event,row)" v-model="row.endtime" placeholder="请输入截止时间" clearable="" :readonly="true"></el-input>
                  </template>
              </vxe-table-column>
              <vxe-table-column field="note" title="备注" width="200px" :edit-render="{ name: 'input', events: { change: hasChange } }" sortable></vxe-table-column>
          </vxe-table>
      </div>
      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="帮助" :visible.sync="dialogHelpVisible" v-if="dialogHelpVisible" width="750px">
          <div v-html="helphtml"></div>
      </el-dialog>

      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="请选择病人类型" :visible.sync="poppattypevisible">

           <rm-pop-common @change="poppattype_change" @cancel="poppattypevisible = false" datatype="pattypeall" dataname="病人类型"></rm-pop-common>
      </el-dialog>

      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="请选择标本类型" :visible.sync="popspecvisible">
          <rm-pop-common @change="popspec_change" @cancel="popspecvisible = false" datatype="specimenaddall" dataname="标本类型"></rm-pop-common>
      </el-dialog>

      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="请选择报告单元" :visible.sync="poprptunitvisible">
          <rm-pop-rptunit @change="poprptunit_change" @cancel="poprptunitvisible = false" width="100%" key="rptunitid" title="请选择报告单元" :visible.sync="poprptunitvisible"></rm-pop-rptunit>
      </el-dialog>

      <!--多选弹窗--->
      <el-dialog width="900px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" :title="multichktitle" :visible.sync="rowdialogvalue" :destroy-on-close="true">
          <rm-SqlMultiple :leftOption="rowoption1" :rightOption="rowoption2" :name="fieldname" :param="condTitle" @change="rowclosedialog"></rm-SqlMultiple>
      </el-dialog>
      <el-dialog v-el-drag-dialog :close-on-click-modal="false" title="设置截止时间" :visible.sync="endtimedialogForm" width="350px" height="200px">
          <el-form :inline="true" @submit.native.prevent label-width="auto" ref="form1" style="border: 1px solid #ccc">
              <table>
                  <tr>
                      <td>工作日截止时间</td>
                      <td>
                          <el-time-picker v-model="workday_endtime" placeholder="任意时间点" value-format="HH:mm" format="HH:mm" style="width:100%"></el-time-picker>
                      </td>
                  </tr>
                  <tr>
                      <td>周末截止时间</td>
                      <td>
                          <el-time-picker v-model="weekend_endtime" placeholder="任意时间点" value-format="HH:mm" format="HH:mm" style="width:100%"></el-time-picker>
                      </td>
                  </tr>
                  <tr>
                      <td>节假日截止时间</td>
                      <td>
                          <el-time-picker v-model="holiday_endtime" placeholder="任意时间点" value-format="HH:mm" format="HH:mm" style="width:100%"></el-time-picker>
                      </td>
                  </tr>
              </table>
          </el-form>
          <div style="text-align: right; padding-top: 5px">
              <el-button type="primary" @click="CommitEndtime()">确定</el-button>
              <el-button @click="endtimedialogForm = false">取消</el-button>
          </div>
      </el-dialog>
      <el-dialog v-el-drag-dialog :close-on-click-modal="false" title="设置开始时间" :visible.sync="starttimedialogForm" width="350px" height="200px">
          <el-form :inline="true" @submit.native.prevent label-width="auto" ref="form1" style="border: 1px solid #ccc">
              <table>
                  <tr>
                      <td>工作日开始时间</td>
                      <td>
                          <el-time-picker v-model="workday_starttime" placeholder="任意时间点" value-format="HH:mm" format="HH:mm" style="width:100%"></el-time-picker>
                      </td>
                  </tr>
                  <tr>
                      <td>周末开始时间</td>
                      <td>
                          <el-time-picker v-model="weekend_starttime" placeholder="任意时间点" value-format="HH:mm" format="HH:mm" style="width:100%"></el-time-picker>
                      </td>
                  </tr>
                  <tr>
                      <td>节假日开始时间</td>
                      <td>
                          <el-time-picker v-model="holiday_starttime" placeholder="任意时间点" value-format="HH:mm" format="HH:mm" style="width:100%"></el-time-picker>
                      </td>
                  </tr>
              </table>
          </el-form>
          <div style="text-align: right; padding-top: 5px">
              <el-button type="primary" @click="CommitStarttime()">确定</el-button>
              <el-button @click="starttimedialogForm = false">取消</el-button>
          </div>
      </el-dialog>
  </div>
</template>

<script>
import XEUtils from "xe-utils";
export default {
  name: "LIS133",
  data() {
      return {
          changeflag: false,
          autonoruleData: [],
          autonoruleLoading: false,
          autonorule: {}, //当前菌落特征
          isdisabeld: true, //特征代码可编辑标志
          rptunit: "",
          validRules: {
              sampleno1: [{ required: true, message: "开始样本号不能为空！" }],
              sampleno2: [{ required: true, message: "结束样本号不能为空！" }],
              rptunitid: [{ required: true, message: "报告单元不能为空！" }],
          },
          keyConfig: {
              isDel: true,
              isTab: true,
              isEdit: true,
          },
          keyLocalConfig: {
              isArrow: true,
          },
          specimenoption: [],
          pattypecodeoption: [],
          rptunitidoption: [],
          ModData: [],
          AddData: [],
          emerflagoption: [
              { label: "All", value: "All" },
              { label: "非急诊", value: "0" },
              { label: "急诊", value: "1" },
          ],
          helpurl: "@/assets/helpdocument/LIS133.mhtml", //LIS133帮助路径
          helphtml: "",
          dialogHelpVisible: false,
          poppattypevisible: false,
          popspecvisible: false,
          poprptunitvisible: false,
          clickrow: null,
          multichktitle: "",
          fieldname:"",
          option1: [],
          option2: [],
          rowoption1:[],
          rowoption2:[],
          condTitle:[],
          rowdialogvalue:false,
          dialogCondvalue: false,
          selectrow :"",
          rptunitendtimeuseflag: false,
          rptunitstarttimeuseflag: false,
          workday_endtime:"",
          weekend_endtime:"",
          holiday_endtime:"",
          endtimedialogForm: false,
          endtime_row:{},
          endtime_code:{},
          workday_starttime:"",
          weekend_starttime:"",
          holiday_starttime:"",
          starttimedialogForm: false,
          starttime_row:{},
          starttime_code:{},
          matchbyoption: [
              { label: "按项目", value: "0" },
              { label: "按分单", value: "1" },
          ],
          reqgroupoption: [], //分单类别
      };
  },
  created() {
      this.InitData();
      this.getUnitList();
  },
  mounted() {},
  methods: {
      getUnitList() {
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS133",
                  method: "post",
                  data: {
                      action: "lab_getUnit",
                  },
              })
              .then((res) => {
                  this.specimenoption = res.specimenoption;
                  this.rptunitidoption = res.rptunitidoption;
                  this.pattypecodeoption = res.pattypecodeoption;
                  this.reqgroupoption = res.reqgroupoption;
              })
              .catch((e) => {
                  this.$message.error(e);
              });
      },
      InitData() {
          this.AddData = [];
          this.ModData = [];
          this.QueryList();
      },
      EditClose({ row, column }) {
          if (column.property == "rptunitid") {
              row.rptunitid = Number(row.rptunitid);
          }
          if (column.property == "reqitemgroups") {
              row.reqitemgroups = this.ResetReqItems(row.reqitemgroups);
          }
          if (column.property == "destreqitemgroups") {
              row.destreqitemgroups = this.ResetReqItems(row.destreqitemgroups);
          }
      },

      curtable_celldblclick({ row, rowIndex, $rowIndex, column, columnIndex, $columnIndex, cell }, event) {
          if (column.property == "pat_typecodename") {
              this.clickrow = row;
              this.rowPopMultipleChk(row.pat_typecode, "pat_typecode", this.pattypecodeoption, "病人类型");
              //this.poppattypevisible = true;
          } else if (column.property == "specimen_name") {
              this.clickrow = row;
              this.popspecvisible = true;
          } else if (column.property == "rptunitname") {
              this.clickrow = row;
              this.poprptunitvisible = true;
          } else if (column.property == "reqitemgroups" && row.matchby == "1") {
              this.clickrow = row;
              this.rowPopMultipleChk(row.reqitemgroups, "reqitemgroups", this.reqgroupoption, "分单类别");
          }
      },

      rowPopMultipleChk(e, fieldname, option, title) {
          let temp = XEUtils.clone(option, true);
          this.rowoption1 = [];
          this.rowoption2 = [];
          if (e != "" && e != null) {
              let k = e.split(",");
              k.forEach((item) => {
                  let ndata = temp.find((m) => m.data == item);
                  if (ndata != undefined && ndata != null) {
                      this.rowoption2.push(ndata);
                  }
              });
              temp.forEach((item) => {
                  if (k.indexOf(item.data + "") < 0) {
                      this.rowoption1.push(item);
                  }
              });
          } else {
              this.rowoption1 = temp;
          }
          this.fieldname = fieldname;
          this.multichktitle = title;
          this.condTitle = title; //!问题出在这里 没更换condTitle
          this.rowdialogvalue = true;
      },

      rowclosedialog(value, name, textvalue, validflag) {
          if (value != "close") {
              if(name == "pat_typecode") {
                  this.clickrow.pat_typecode = value == "" ? "ALL" : value;
                  this.clickrow.pat_typecodename = textvalue == ""?"ALL":textvalue;
              } else if (name == "reqitemgroups") {
                  this.clickrow.reqitemgroups = value;
              }
              this.changeflag = true;
          }
          this.rowdialogvalue = false;
      },

      poppattype_change(c, n) {
          this.poppattypevisible = false;
          this.clickrow.pat_typecode = c;
          this.clickrow.pat_typecodename = n;
          this.changeflag = true;
      },
      popspec_change(c, n) {
          this.popspecvisible = false;
          this.clickrow.specimen_code = c;
          this.clickrow.specimen_name = n;
          this.changeflag = true;
      },
      poprptunit_change(c, n) {
          this.poprptunitvisible = false;
          this.clickrow.rptunitid = c;
          this.clickrow.rptunitname = n;
          this.changeflag = true;
      },

      rowClassName({ row, column }) {
          if (row.operation == "2") {
              return "Update";
          }
          if (row.operation == "1") {
              return "Add";
          }
      },
      hasChange({ row }) {
          if (row.operation != "1") row.operation = "2";
          this.changeflag = true;
      },
      CommitStarttime(){
          var workday_starttime="";
          var weekend_starttime="";
          var holiday_starttime="";

          //把值拼成一个
          if(this.workday_starttime){
              workday_starttime=this.workday_starttime;
          }
          if(this.weekend_starttime){
              weekend_starttime=this.weekend_starttime;
          }
          if(this.holiday_starttime){
              holiday_starttime=this.holiday_starttime;
          }
          var starttime = "工作日:" + workday_starttime + "|周末:" + weekend_starttime + "|节假日:" + holiday_starttime;


          if(starttime!="工作日:|周末:|节假日:"){
              if(this.rptunitstarttimeuseflag) this.rptunitstarttime = starttime;
              else this.starttime_row.starttime = starttime;
          } else {
              if(this.rptunitstarttimeuseflag) this.rptunitstarttime = "";
              else this.starttime_row.starttime = "";
          }

          this.starttimedialogForm=false;

          this.workday_starttime="";
          this.weekend_starttime="";
          this.holiday_starttime="";
          this.changeflag = true;
      },
      CommitEndtime(){
          var workday_endtime="";
          var weekend_endtime="";
          var holiday_endtime="";

          //把值拼成一个
          if(this.workday_endtime){
              workday_endtime=this.workday_endtime;
          }
          if(this.weekend_endtime){
              weekend_endtime=this.weekend_endtime;
          }
          if(this.holiday_endtime){
              holiday_endtime=this.holiday_endtime;
          }
          var endtime = "工作日:" + workday_endtime + "|周末:" + weekend_endtime + "|节假日:" + holiday_endtime;


          if(endtime!="工作日:|周末:|节假日:"){
              if(this.rptunitendtimeuseflag) this.rptunitendtime = endtime;
              else this.endtime_row.endtime = endtime;
          } else {
              if(this.rptunitendtimeuseflag) this.rptunitendtime = "";
              else this.endtime_row.endtime = "";
          }

          this.endtimedialogForm=false;

          this.workday_endtime="";
          this.weekend_endtime="";
          this.holiday_endtime="";
          this.changeflag = true;
      },
      setstarttime(code, row) {
          this.rptunitstarttimeuseflag = false;
          this.starttime_row=row;
          this.starttime_code=code;

          var times =row.starttime;
          if(times && times.indexOf("|")!=-1){
              var timesArr=times.split("|");
              this.workday_starttime=timesArr[0].replace(/工作日:/, "");;
              this.weekend_starttime=timesArr[1].replace(/周末:/, "");
              this.holiday_starttime=timesArr[2].replace(/节假日:/, "");
          }

          this.starttimedialogForm=true;
      },
      setendtime(code, row) {
          this.rptunitendtimeuseflag = false;
          this.endtime_row=row;
          this.endtime_code=code;

          var times =row.endtime;
          if(times && times.indexOf("|")!=-1){
              var timesArr=times.split("|");
              this.workday_endtime=timesArr[0].replace(/工作日:/, "");;
              this.weekend_endtime=timesArr[1].replace(/周末:/, "");
              this.holiday_endtime=timesArr[2].replace(/节假日:/, "");
          }

          this.endtimedialogForm=true;
      },
      matchbyChange(e, val) {
          e.row.matchby = val.target.value;
          e.row.reqitemgroups = "";
          this.changeflag = true;
      },
      rptunitChange(code, name) {
          this.changeflag = true;
      },

      format({ cellValue, row, column }) {
          if (column.property == "pat_typecodename") {
              if (row.pat_typecode == "All") {
                  return "All";
              } else {
                  return row.pat_typecodename;
              }
          }
          if (column.property == "specimen_name") {
              if (row.specimen_code == "All") {
                  return "All";
              } else {
                  return row.specimen_name;
              }
          }
      },

      QueryList() {
          this.autonoruleLoading = true;
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS133",
                  method: "post",
                  data: {
                      action: "querylist",
                      rptunit: this.rptunit,
                  },
              })
              .then((res) => {
                  if (res.data) {
                      let data = res.data;
                      data.forEach((row) => {
                          if(row.endtime){
                              var tempendtime = row.endtime.split("|");
                              if (tempendtime.length > 0 && tempendtime[0] != "") {
                                  row.endtime = "工作日:" + tempendtime[0] + "|周末:" + tempendtime[1] + "|节假日:" + tempendtime[2];
                              }
                              else{
                                  row.endtime = "";
                              }
                          }
                          if(row.starttime){
                              var tempstarttime = row.starttime.split("|");
                              if (tempstarttime.length > 0 && tempstarttime[0] != "") {
                                  row.starttime = "工作日:" + tempstarttime[0] + "|周末:" + tempstarttime[1] + "|节假日:" + tempstarttime[2];
                              }
                              else{
                                  row.starttime = "";
                              }
                          }
                      });
                      this.autonoruleData = data;
                      if (data.length > 0) {
                          this.autonorule = XEUtils.clone(data[0], true);
                          this.$refs.lxTable.setCurrentRow(data[0]);
                      } else {
                          this.utype = 1;
                      }
                  } else {
                      this.utype = 1;
                  }
                  this.autonoruleLoading = false;
              })
              .catch(() => {
                  this.autonoruleLoading = false;
              });
      },
      SaveAction() {
          this.$refs.lxTable.validate((valid) => {
              if (valid) {
                  return new Promise((s, f) => {
                      let data = this.$refs.lxTable.getRecordset();
                      let newdata = this.$refs.lxTable.getTableData().fullData;
                      this.$tool
                          .request({
                              url: "LIS/LIS1/LIS133",
                              method: "post",
                              data: {
                                  action: "savedata",
                                  autonoruledet: data,
                              },
                          })
                          .then((res) => {
                              if (res.errcode == "1") {
                                  this.$message.success("保存成功");
                                  this.changeflag = false;
                                  //this.$refs.lxTable.loadData(newdata);
                                  this.QueryList();
                                  s();
                              } else {
                                  this.$messge.error(res.errmsg);
                                  f();
                              }
                          })
                          .catch((e) => {
                              this.$message.error(e);
                          });
                  });
              } else {
                  this.$message({ type: "error", message: "校验不通过！" });
              }
          });
      },
      Cancel() {
          this.$confirm("您确定要取消这次改动吗？", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
          })
              .then(() => {
                  this.changeflag = false;
                  this.QueryList();
              })
              .catch((e) => {
                  this.$message.error(e);
              });
      },
      ResetReqItems(itemstr) {
          var itemarr = itemstr.split(",");
          var restr = "";

          for (let i = 0; i < itemarr.length; i++) {
              var sortstr = itemarr[i].split("+");
              sortstr = sortstr.sort((a, b) => a.localeCompare(b));
              var mergestr = sortstr.join("+");
              if (i < itemarr.length - 1) restr += mergestr + ",";
              else restr += mergestr;
          }
          return restr.toUpperCase();
      },
      HelpInit() {
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS133",
                  method: "post",
                  data: {
                      action: "helpdoc",
                  },
              })
              .then((res) => {
                  this.helphtml = res;
                  this.dialogHelpVisible = true;
              });
      },
      AddDetail() {
          let xTable1date = this.$refs.lxTable.tableData;
          let len = (xTable1date.length + 1) * 10;
          let record = {
              pat_typecode: "All",
              pat_typecodename: "All",
              specimen_code: "All",
              specimen_name: "All",
              emer_flag: "All",
              reqitemgroups: "All",
              seq: len,
              destreqitemgroups: "All",
              operation: "1",
              weekdays: "",
              endtime: "",
              starttime: "",
              matchby: "0",
          };
          this.$refs.lxTable.insert(record, -1);
          this.changeflag = true;
      },
      DelDetail() {
          var row = this.$refs.lxTable.getCurrentRow();
          if (row) {
              let havedata = this.$refs.lxTable.tableData;
              var rowindex = havedata.indexOf(row);
              this.$refs.lxTable.remove(row);
              if (havedata.length > 0) {
                  if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                  let selectrow = havedata[rowindex];
                  this.$refs.lxTable.setCurrentRow(selectrow);
              }
              this.changeflag = true;
          } else {
              this.$messge.warning("请选择要修改的自动编号规则!");
          }
      },
      //导出
      Export() {
          let p = { prgno: "LIS113" };
          this.$pub.commexport(this, p, `自动编号规则.dat`);
      },
  },
};
</script>

<style scoped>
::v-deep .Update {
  color: #5050fa;
}
::v-deep .Add {
  color: #b47c7c;
}
.pagetop {
  width: 100%;
  height: 35px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  z-index: 99;
}
.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}
.search-title {
  margin: 0 5px 0 20px;
  font-size: 12px;
}
.top-deal {
  right: 5px;
  position: absolute;
  top: 5px;
  display: flex;
  align-items: center;
}
.middle-center {
  position: absolute;
  height: 100%;
  right: 605px;
  left: 0;
}

.middle-right {
  position: absolute;
  top: 0;
  bottom: 2px;
  overflow: hidden;
  width: 600px;
  right: 0px;
}
::v-deep .el-button.is-disabled {
  border: 0px;
  color: grey;
  background-color: #e6e6e6;
  font-weight: normal;
}
</style>
